package com.mhsoft.fenak.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.NameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.net.ConnectivityManager;

public class ConnectionManager {

	// ===========================================================================================================
	// isInternetAvailable(Context context) ======================================================================
	// ===========================================================================================================
	public static boolean isInternetAvailable(Context context) {

		boolean flag = false;

    	ConnectivityManager connec = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    	
    	if (connec != null && (connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED) ||
        		(connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED)){
    		
    		flag = true;
    	}
    	else if (connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED ||  
        		connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED ) {             
            //Not connected.        
    		flag = false;
        }
    	
    	return flag;
	}
	
	// ===========================================================================================================
	// getResponseString(InputStream input) ======================================================================
	// ===========================================================================================================
	public static String getResponseString(InputStream input) throws IllegalStateException, IOException {
	
		String responseString = "";
		BufferedReader in = null;
		try {
			in = new BufferedReader(new InputStreamReader(input));
			StringBuffer sb = new StringBuffer("");
			String line = "";
			String NL = System.getProperty("line.separator");
			while ((line = in.readLine()) != null) {
				sb.append(line + NL);
			}
			in.close();
			responseString = sb.toString();
		
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return responseString;
	}
	
	// ===========================================================================================================
	// doPostRequest =============================================================================================
	// ===========================================================================================================
	public static void doPostRequest(final Activity activity, String progressTitle, String progressMsg
			, String url, String anyNumber,ArrayList<NameValuePair> data){
		
		
		final MyHttpPostAsyncTask asyncTask = new MyHttpPostAsyncTask(activity);
		
		ProgressDialog p = ProgressDialog.show(activity, progressTitle, progressMsg);
		p.setCancelable(true);
		p.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				
				asyncTask.cancel(true);
				activity.onBackPressed();
			}
		});
		
		asyncTask.execute(url, anyNumber, data, p);
	}
	
	// ===========================================================================================================
	// doGetRequest ==============================================================================================
	// ===========================================================================================================
	public static void doGetRequest(final Activity activity, String progressTitle, String progressMsg
			, String url, String anyNumber){
		
		final MyHttpGetAsyncTask asyncTask = new MyHttpGetAsyncTask(activity);
		
		ProgressDialog p = ProgressDialog.show(activity, progressTitle, progressMsg);
		p.setCancelable(true);
		p.setOnCancelListener(new OnCancelListener() {
			
			@Override
			public void onCancel(DialogInterface dialog) {
				
				asyncTask.cancel(true);
				activity.onBackPressed();
			}
		});
		
		asyncTask.execute(url, anyNumber, p);
	}
}
